package com.sad.jetpack.v1.thememanager.api;

import android.content.Context;

import androidx.annotation.StyleRes;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class BasicThemeDesHelper {

    private final static Map<Integer,BasicThemeDes> BASIC_THEME_DES_MAP=new LinkedHashMap<>();
    private final static Map<Integer,Integer> BASIC_THEME_STYLE_RES_ID=new LinkedHashMap<>();
    public static void initInsideBasicTheme(Context context){
        //预置的基础主题
        BasicThemeDes btd_day=new BasicThemeDes(0,"日间主题",0);
        BasicThemeDes btd_night=new BasicThemeDes(0,"夜间主题",1);
        BasicThemeDes btd_BGDRed=new BasicThemeDes(0,"勃艮第红主题",2);
        addBasicThemeDesRes(btd_day,R.style.ThemeSad_White);
        addBasicThemeDesRes(btd_night,R.style.ThemeSad_Night);
        addBasicThemeDesRes(btd_BGDRed,R.style.ThemeSad_BGDRed);
    }

    public static BasicThemeDes findBasicThemeDesByCode(@StyleRes int code){
        return BASIC_THEME_DES_MAP.get(code);
    }

    public static @StyleRes int findResIdByBasicThemeDesCode(int code){
        Integer id = BASIC_THEME_STYLE_RES_ID.get(code);
        if (id==null){
            return -1;
        }
        return id;
    }
    public static @StyleRes int findResIdByLocalTheme(LocalTheme localTheme){
        if (localTheme==null){
            return -1;
        }
        BasicThemeDes basicThemeDes=localTheme.getBasicThemeDes();
        if (basicThemeDes==null){
            return -1;
        }
        int code=basicThemeDes.getCode();
        return findResIdByBasicThemeDesCode(code);
    }

    public static void addBasicThemeDesRes(BasicThemeDes basicThemeDes, @StyleRes int newThemeResId){
        if (basicThemeDes!=null){
            int code=basicThemeDes.getCode();
            BASIC_THEME_DES_MAP.put(code,basicThemeDes);
            BASIC_THEME_STYLE_RES_ID.put(code,newThemeResId);
        }
    }

    public static BasicThemeDes findBasicThemeDesByThemeResId(@StyleRes int id){
        AtomicReference<BasicThemeDes> atomicDes=new AtomicReference<>(null);
        travelAllThemeDes(new OnTravelAllThemeDesCallback() {
            @Override
            public boolean onTraveling(BasicThemeDes basicThemeDes) {
                int tid=findResIdByBasicThemeDesCode(basicThemeDes.getCode());
                boolean found=tid==id;
                if (found){
                    atomicDes.set(basicThemeDes);
                }
                return found;
            }
        });
        return atomicDes.get();
    }

    public interface OnTravelAllThemeDesCallback{
        boolean onTraveling(BasicThemeDes basicThemeDes);
    }

    public static void travelAllThemeDes(OnTravelAllThemeDesCallback callback){
        Iterator<Map.Entry<Integer, BasicThemeDes>> iterator=BASIC_THEME_DES_MAP.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, BasicThemeDes> entityEntry=iterator.next();
            int k=entityEntry.getKey();
            BasicThemeDes v=entityEntry.getValue();
            if (v!=null){
                if (callback!=null){
                    boolean isBreak=callback.onTraveling(v);
                    if (isBreak){
                        break;
                    }
                }
            }
        }
    }
}
